package com.ccsoft.zknote.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
@Configuration
@EnableScheduling
public class ScheduleUtils {
    @Lazy
    @Autowired
    private DynamicScheduledTaskRegistrar taskRegistrar;

    @PostConstruct
    public void init(){
        // 动态添加定时任务
//        taskRegistrar.addCronTask("定时任务2秒","0/2 * * * * ?",() -> {
//            try {
//                updateAreaQrCode();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        });
//
//        taskRegistrar.addCronTask("定时任务1秒","0/1 * * * * ?",() -> {
//            try {
//                updateAreaQrCode1();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        });
//
//        List<String> allTaskName = taskRegistrar.getAllTaskName();
//        System.out.println(allTaskName);
    }

    //@Scheduled(cron = "0/2 * * * * ?")
    private void updateAreaQrCode() throws InterruptedException {
        System.out.println(DatetimeUtils.formatNow() + " - 2秒定时任务开始");
        Thread.sleep(3000);
        System.out.println(DatetimeUtils.formatNow() + " - 2秒定时任务结束");
    }

    //@Scheduled(cron = "0/1 * * * * ?")
    private void updateAreaQrCode1() throws InterruptedException {
        System.out.println(DatetimeUtils.formatNow() + " - 1秒定时任务");
    }
}
